<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/** 
 * 商品套装ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

require 'module/base/promotionHandler.class.php';
require 'module/base/goodsHandler.class.php';

class ajax_package {
	//入口
	public function execute(){
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		switch ($operFlg){
			case 1://显示商品套装列表
				$this->showPackageList();
				break;
			case 2:
			case 3://商品套装编辑
				$this->editPackage();
				break;
			case 4://删除商品套装
				$this->deletePackage();
				break;
			case 5://根据输入的商品编号得到商品信息
				$this->getGoodsInfo();
				break;
			case 6://启用禁用
				$this->changeUseFlg();
				break;
			case 7:
				$this->getPackageInfo();
				break;
			default:
				echo json_encode(array('code'=>400,'message'=>'operFlg传值错误'));
				break;
		}
	}
	//套装列表
	private function showPackageList () {
		global $G_PGLOB;
		//当前页
		$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
		
		//每页显示条数
		$rp = isset($_REQUEST['rp']) ? $_REQUEST['rp'] : 10;
		
		//排序的列的名称
		$sortname = isset($_REQUEST['sortname']) ? $_REQUEST['sortname'] : 'goods_group_id';
		
		//排序方式（正序、倒序）
		$sortorder = isset($_REQUEST['sortorder']) ? $_REQUEST['sortorder'] : 'ASC';
		
		//套装名称
		$goods_group_name = isset($_REQUEST['goods_group_name']) ? $_REQUEST['goods_group_name'] : '';
		
		//活动日期
		$date = isset($_REQUEST['date']) ? $_REQUEST['date'] : '';
		
		//商品套装号
		$goods_group_id = (isset($_REQUEST['goods_group_id']) && is_numeric($_REQUEST['goods_group_id'])) ? $_REQUEST['goods_group_id'] : '';
		
		$importData = array('page' 				=> $page,
							'rp'                => $rp,
							'sortname'          => $sortname,
							'sortorder'         => $sortorder,
							'goods_group_name'	=> $goods_group_name,
							'date'				=> $date);
		
		$promotionHandler = new promotionHandler($G_PGLOB->DBCA);
		
		$result_group = $promotionHandler->searchGroup( $importData );
		
		//页面输出数组
		$result_array = array(
				'page' => $page,
				'total' => $result_group['message']['total'],
				'rows' => array()
		);
		
		if ($result_group['code'] == 200 && !empty($result_group['message']['data'])){
			foreach ($result_group['message']['data'] as $groupList){
				
				if ($groupList->useFlg == 0){
					$useFlg = '禁用';
				}elseif ($groupList->useFlg == 1){
					$useFlg = '启用';
				}
				
				$result_array['rows'][] = array(
						'id' => $groupList->goods_group_id,
						'cell' => array(
								'<a href="javascript:void(0);" onclick="source.detail('.$groupList->goods_group_id.');">修改</a>&nbsp;&nbsp;'.
								'<a href="javascript:void(0);" onclick="source.edit(this,'.$groupList->goods_group_id.');">'.$useFlg.'</a>',
								'<a href="javascript:void(0);" onclick="source.info('.$groupList->goods_group_id.');">详情</a>',
								$groupList->goods_group_id,
								$groupList->goods_group_name,
								$groupList->goods_group_price,
								$groupList->goods_group_points,
								$groupList->startDate,
								$groupList->endDate
						)
				);
				
			}
		}
		
		echo json_encode($result_array);
		
	}
	
	private function getGoodsInfo (){
		global $G_PGLOB;
		//商品编码
		$product_code = isset($_REQUEST['product_code']) ? trim($_REQUEST['product_code']) : '';
		
		if (empty($product_code)) {
			$result_array = array('code'=>400,'message'=>'请输入商品编号');
			echo json_encode($result_array);
			die();
		}
		
        $goodsHandler = new goodsHandler($G_PGLOB->DBCA);
        $arg_array = array(	'product_code' 	=> $product_code,
            'useFlg'		=> 1,
            'forsale'		=> 1);
        $result_search = $goodsHandler->searchGoodsList($arg_array);

		
		if (empty($result_search)) {
			$result_array = array('code'=>400,'message'=>'该商品不存在或无库存或已下架');
			echo json_encode($result_array);
			die();
		}

		$result_array = array(	'code'		=> 200,
								'message'	=> array(	'product_code'	=> $result_search[0]['product_code'],
														'goodsName'		=> $result_search[0]['goodsName'],
														'spec'			=> $result_search[0]['spec'],
														'salePrice'		=> $result_search[0]['salePrice'],
														'goodsCount'	=> 1,
														'salePoints'	=> $result_search[0]['salePoints'],
														'discountPrice'	=> $result_search[0]['salePoints'],
														'postID'		=> $result_search[0]['postID'],
														'goodsID'		=>$result_search[0]['goodsID'],
														'link'		=>$result_search[0]['link'],
														'img_link'		=>$result_search[0]['img_link'],
                                )
        );
		
		echo json_encode($result_array);
	}
	
	//编辑、新增套餐
	private function editPackage () {
		global $G_PGLOB;

		//operFlg(2新增，3修改)
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		//主商品
		$mainInfo = isset($_REQUEST['mainInfo']) ? $_REQUEST['mainInfo'] : null;
		//套装商品
		$groupGoodsInfo = isset($_REQUEST['groupGoodsInfo']) ? $_REQUEST['groupGoodsInfo'] : null;
		
		$promotionHandler = new promotionHandler($G_PGLOB->DBCA);
		
		$importData = array('operFlg'        => $operFlg,
							'creator'        => $G_PGLOB->currentUserID,
							'editor'         => $G_PGLOB->currentUserID,
							'mainInfo' 		 => $mainInfo,
							'groupGoodsInfo' => $groupGoodsInfo);

        error_log(print_r($importData,true));
		
		$result_edit = $promotionHandler->editPackage($importData);
		
		echo json_encode($result_edit);
	}
	
	//启用、禁用
	private function changeUseFlg () {
		global $G_PGLOB;
		
		$goods_group_id = isset($_REQUEST['goods_group_id']) ? $_REQUEST['goods_group_id'] : null;
		
		$changeFlg = isset($_REQUEST['changeFlg']) ? $_REQUEST['changeFlg'] : null;
		
		if (empty($goods_group_id) || !in_array($changeFlg, array(0,1))){
			$return_arr = array('code'=>400, 'message'=>'页面传值错误');
			echo json_encode($return_arr);
			die();
		}
		
		try {
			
			$group_obj = new entity('promotion_group');
			$group_obj->goods_group_id = $goods_group_id;
			$group_obj->useFlg = $changeFlg;
			if (!$G_PGLOB->DBCA->fetch($group_obj)){
				$return_arr = array('code'=>400, 'message'=>'您的页面已过期，请刷新页面');
				echo json_encode($return_arr);
				die();
			}
			$group_obj->useFlg = ($changeFlg == 1) ? 0 : 1;
			
			$msg = ($changeFlg == 1) ? '该套装已禁用' : '该套装已启用';
			
			$G_PGLOB->DBCA->save($group_obj);
			
			$return_arr = array('code'=>200, 'message'=>$msg);
			echo json_encode($return_arr);
			
		}catch (Exception $e){
			$msg = $e->getMessage();
			$return_arr = array('code'=>500, 'message'=>$msg);
			echo json_encode($return_arr);
		}
		
	}
	
	//删除套装
	private function deletePackage () {
		global $G_PGLOB;
		
		$groupIDArr = isset($_REQUEST['groupIDArr']) ? $_REQUEST['groupIDArr'] : null;
		
		$promotionHandler = new promotionHandler($G_PGLOB->DBCA);
		
		$result_del = $promotionHandler->deletePackage($groupIDArr);
		
		echo json_encode($result_del);
	}
	
	//根据套装号得到商品套装详情
	private function getPackageInfo (){
		global $G_PGLOB;
		
		$goods_group_id = isset($_REQUEST['goods_group_id']) ? $_REQUEST['goods_group_id'] : null;
		
		$promotionHandler = new promotionHandler($G_PGLOB->DBCA);
		
		$arg_array = array('goods_group_id'=>$goods_group_id);
		
		$result_search = $promotionHandler->searchGroupDetailAll($arg_array);
		
		//页面输出数组
		$result_array = array(
				'page' => '',
				'total' => '',
				'rows' => array()
		);
		
		if (!empty($result_search['message']['data'])){
			foreach ($result_search['message']['data'] as $detailList){
				
				$result_array['rows'][] = array(
						'id' => $detailList->product_code,
						'cell'=>array(
								$detailList->product_code,
								$detailList->goodsName,
								$detailList->spec,
								$detailList->salePrice,
								$detailList->discountPrice,
								$detailList->goodsCount,
								$detailList->salePoints
								)
						);
			}
		}
		echo json_encode($result_array);
	}
	
}